// 引入koa-router
const Router = require('koa2-router');
const router = new Router();

const { timeTrans, formatPrice } = require("../../utils");
const {
  getGoods,
  getGoodsAll,
  addGoods,
  updateGoods,
  deleteGoods,
  showGoods,
  batchDelete
} = require("../../model/admin/goods");

/* goods增删改查 */

// 查找goods
router.get("/",async (ctx) => {
  // console.log(ctx.request.query);
  let num = Number(ctx.request.query.currentPage);
  ctx.body = await getGoods([num]);
});

// 添加goods
router.post("/add",async (ctx) => {
  const goods_id = Number(ctx.request.body.goods_id);
  // 排序值去重
  const data = await getGoodsAll();
  let isGoodsId = data.some(item=>{
    return item.goods_id === goods_id;
  })
  if (isGoodsId){
    ctx.body = {
      code: 500,
      msg: "排序值重复，请重新输入"
    };
  }else {
    const category = Number(ctx.request.body.category);
    const arr = await getGoods(category);
    if (arr.length<4){
      const title = ctx.request.body.title;
      const price = formatPrice(ctx.request.body.price);
      const src = "http://localhost/images/home/" + ctx.request.body.src.name;
      const url = "#/product?id=" + ctx.request.body.product_id;
      const atime = timeTrans(ctx.request.body.atime);

      // console.log(ctx.request);
      // console.log(goods_id,title,price,src,url,category,atime);
      // ctx.body = 'ok';

      try {
        await addGoods([goods_id,title,price,src,url,category,atime]);
        ctx.body = {
          code: 200,
          msg: "添加成功",
        };
      }catch (err) {
        ctx.body = {
          code: 500,
          msg: "添加失败",
          err,
        };
      }
    }else {
      ctx.body = {
        code: 500,
        msg: "当前类别中商品数量大于或等于4,无法进行添加"
      };
    }

  }
});

// 修改goods
router.post("/update",async (ctx) => {
  const goods_id = Number(ctx.request.body.goods_id);
  const title = ctx.request.body.title;
  const price = formatPrice(ctx.request.body.price);
  const src = "http://localhost/images/home/" + ctx.request.body.src.name;
  const url = "#/product?id=" + ctx.request.body.product_id;
  const category = Number(ctx.request.body.category);
  const atime = timeTrans(ctx.request.body.atime);

  try {
    await updateGoods([title,price,src,url,category,atime,goods_id]);
    ctx.body = {
      code: 200,
      msg: "修改成功",
    };
  }catch (err) {
    ctx.body = {
      code: 500,
      msg: "修改失败",
      err,
    };
  }
});

// 删除单个goods
router.get("/delete",async (ctx) => {
  // console.log(req.query);
  const goods_id = Number(ctx.request.query.goods_id);

  try {
    await deleteGoods([goods_id]);
    ctx.body = {
      code: 200,
      msg: "删除成功",
    };
  }catch (err) {
    ctx.body = {
      code: 500,
      msg: "删除失败",
      err,
    };
  }
});

// 是否展示goods
router.get("/show", (ctx) => {
  // console.log(req.query);
  const isShow = ctx.request.query.isShow !== "false";
  const goods_id = Number(ctx.request.query.goods_id);

  try {
    showGoods([isShow, goods_id]);
    ctx.body = {
      code: 200,
      msg: "修改成功",
    };
  }catch (err) {
    ctx.body = {
      code: 500,
      msg: "修改失败",
      err,
    };
  }
});

// 批量删除
router.post("/batchDelete",async (ctx) => {
  const arr = ctx.request.body.arr;
  arr.forEach((item,index,arr)=>{
    arr[index] = Number(item);
  })
  // ctx.body = 'ok'
  try {
    await batchDelete([arr]);
    ctx.body = {
      code:200,
      msg:"删除成功"
    };
  }catch (err) {
    // console.log(err);
    ctx.body = {
      code:500,
      msg:"删除失败",
      err
    };
  }

});

module.exports = router;
